import pandas as pd

df = pd.read_csv("static/data/info_pre.csv")
df_line = pd.read_csv("static/data/line.csv")
region_list = ['荔湾区', '越秀区', '海珠区', '天河区', '白云区', '黄埔区', '番禺区', '花都区', '南沙区', '从化区',
               '增城区']


def geo_data():
    data = [{'name': i[0], 'value': i[1]} for i in
            df[df['region'].isin(region_list)][['name', 'region']].groupby(['region']).count().reset_index().values]
    return {
        'data': data
    }


def pie_data():
    values_list = df[['name', 'type']].groupby('type').count().reset_index().values
    data = [{'name': i[0], 'value': i[1]} for i in values_list]
    return {
        'data': data
    }


def word_cloud_data():
    word_list = '|'.join(df['tag'].values.tolist()).split('|')
    df_word = pd.DataFrame({'word': word_list})
    df_word = df_word.groupby('word').value_counts().reset_index().rename(columns={0: 'count'})[
        ['word', 'count']].sort_values(by='count', ascending=False)
    data = [{'name': i[0], 'value': i[1]} for i in df_word.values]
    return {
        'data': data
    }


def heatmap_data():
    data = []
    price_list = [f'{10000 * (i - 1)}-{10000 * i}' for i in range(1, 6)]
    for i in range(1, 6):
        df_heatmap = df[(df['main_price'] > 10000 * (i - 1)) & (df['main_price'] < 10000 * i)]
        for j in range(len(region_list)):
            count = len(df_heatmap[df_heatmap['region'] == region_list[j]])
            data.append([i - 1, j, count])
    return {
        'price_list': price_list,
        'region_list': region_list,
        'data': data
    }


def scatter_data():
    df_scatter = df.drop(df[df['total_price_mean'] == 0].index)
    df_scatter = df_scatter.drop(df_scatter[df_scatter['area_mean'] == 0].index)
    data = df_scatter[['total_price_mean', 'area_mean']].values.tolist()
    line = df_line.values.tolist()
    return {
        'data': data,
        'line': line
    }


def bar_data():
    house_type_list = '|'.join(df['house_type'].values.tolist()).split('|')
    df_house_type = pd.DataFrame({'house_type': house_type_list})
    df_house_type = df_house_type.groupby('house_type').value_counts().reset_index().rename(columns={0: 'count'})[
        ['house_type', 'count']].sort_values(by='count', ascending=False)
    name = [i[0] for i in df_house_type.values]
    value = [i[1] for i in df_house_type.values]
    return {
        'name': name,
        'value': value
    }
